#pragma once
#include <QObject>
#include <QTimer>
#include "gologic.h"

// 把 GoLogic 包装成 QML 能用的模型
class GoBoard : public QObject {
    Q_OBJECT
    Q_PROPERTY(int size READ size CONSTANT)
    Q_PROPERTY(QVector<int> boardData READ boardData NOTIFY boardChanged)
    Q_PROPERTY(int blackCaptures READ blackCaptures NOTIFY capturedChanged)
    Q_PROPERTY(int whiteCaptures READ whiteCaptures NOTIFY capturedChanged)
    Q_PROPERTY(bool isBlackTurn READ isBlackTurn NOTIFY turnChanged)
    Q_PROPERTY(int gameMode READ gameMode WRITE setGameMode NOTIFY gameModeChanged)
public:
    explicit GoBoard(QObject* parent = nullptr);

    Q_INVOKABLE bool playStone(int r, int c);
    Q_INVOKABLE bool playStone(int r, int c, bool black) {
        return m_logic.playStone(r, c, black);
    }
    Q_INVOKABLE void undo() { m_logic.undo(); }
    Q_INVOKABLE void reset() { m_logic.reset(); }
    Q_INVOKABLE QString score() const { return m_logic.score(); }
    Q_INVOKABLE int scoreDelta() const { return m_logic.scoreDelta(); }
    Q_INVOKABLE QString finalScore() const { return m_logic.finalScore(); }
    Q_INVOKABLE int gameMode() const { return m_logic.gameMode(); }
    Q_INVOKABLE void setGameMode(int mode) { m_logic.setGameMode(mode); }

    int size() const { return m_logic.size(); }
    QVector<int> boardData() const;
    int blackCaptures() const { return m_black; }
    int whiteCaptures() const { return m_white; }
    bool isBlackTurn() const { return m_logic.isBlackTurn(); }

signals:
    void boardChanged();
    void capturedChanged();
    void turnChanged();
    void gameModeChanged();

private slots:
    void onTurnChanged();
    void makeAIMove();

private:
    GoLogic m_logic{13};  // 修改为13x13棋盘
    int m_black = 0, m_white = 0;
    QTimer m_aiTimer;  // AI延迟定时器
};
